<script setup>
import { ref, watch, onMounted } from 'vue'
import axios from 'axios'
import { ElMessage } from 'element-plus'

/* 父组件传入当前挂号记录 */
const props = defineProps({
  patient: Object               // Register 对象
})

/* ---------- 表单模板 ---------- */
const blank = () => ({
  id: null,
  casenumber: '',     // 病历号（后端自动赋值，这里只是显示）
  registid: null,     // 挂号 ID
  readme: '',
  present: '',
  presenttreat: '',
  history: '',
  allergy: '',
  physique: '',
  proposal: '',
  careful: '',
  checkresult: '',
  diagnosis: '',
  handling: '',
  casestate: 1        // 1-暂存 2-提交 3-诊毕
})

/* 响应式表单对象 */
const form = ref(blank())

/* ---------- 载入病历 ---------- */
async function loadRecord (registId) {
  const { data } = await axios.get(`/api/doctor/record/${registId}`)
  Object.assign(form.value, blank(), data)   // data 可能是空对象
}

/* ---------- 保存 ---------- */
async function save (state = 1) {
  form.value.casestate = state               // 1-暂存 2-提交
  await axios.post('/api/doctor/record', form.value)
  ElMessage.success(state === 1 ? '暂存成功' : '提交成功')
  await loadRecord(form.value.registid)      // 重新拉取，刷新界面
}

/* ---------- 监听患者切换 ---------- */
watch(() => props.patient, p => {
  if (p) loadRecord(p.id)
}, { immediate: true })
</script>

<template>
  <el-form :model="form" label-width="100px" class="mrh-form">
    <el-row :gutter="20">
      <el-col :span="12">
        <el-form-item label="主诉">
          <el-input type="textarea" v-model="form.readme" rows="2" />
        </el-form-item>
        <el-form-item label="现病史">
          <el-input type="textarea" v-model="form.present" rows="2" />
        </el-form-item>
        <el-form-item label="现病治疗">
          <el-input type="textarea" v-model="form.presenttreat" rows="2" />
        </el-form-item>
        <el-form-item label="既往史">
          <el-input type="textarea" v-model="form.history" rows="2" />
        </el-form-item>
        <el-form-item label="过敏史">
          <el-input type="textarea" v-model="form.allergy" rows="2" />
        </el-form-item>
      </el-col>

      <el-col :span="12">
        <el-form-item label="体格检查">
          <el-input type="textarea" v-model="form.physique" rows="2" />
        </el-form-item>
        <el-form-item label="检查建议">
          <el-input type="textarea" v-model="form.proposal" rows="2" />
        </el-form-item>
        <el-form-item label="注意事项">
          <el-input type="textarea" v-model="form.careful" rows="2" />
        </el-form-item>
        <el-form-item label="检查结果">
          <el-input type="textarea" v-model="form.checkresult" rows="2" />
        </el-form-item>
        <el-form-item label="诊断结果">
          <el-input type="textarea" v-model="form.diagnosis" rows="2" />
        </el-form-item>
        <el-form-item label="处理意见">
          <el-input type="textarea" v-model="form.handling" rows="2" />
        </el-form-item>
      </el-col>
    </el-row>

    <el-form-item class="text-right">
      <el-button type="primary" @click="save(1)">暂存</el-button>
      <el-button type="success" @click="save(2)">提交</el-button>
    </el-form-item>
  </el-form>
</template>

<style scoped>
.mrh-form { padding: 10px 20px; }
</style>
